import BaseUtil from '@/BaseUtil';
import MathUtil from '@/MathUtil';
import SelectUtil from '@/SelectUtil';
import SelectCommon from 'code/Core/Common/SelectCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 气功波
 **/
export default class WS_AirBolt extends AbilityBase {
    static instance: WS_AirBolt;
    constructor(id: string = 'Azqb') {
        super(id);
        WS_AirBolt.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {}
    pool: Map<unit, unit[]> = new Map();
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let ag = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), tarX, tarY);
        let total = 60;
        let cx = GetUnitX(source);
        let cy = GetUnitY(source);
        let ef = CreateUnit(GetOwningPlayer(source), 'ewnv', cx, cy, ag);
        DzSetUnitModel(ef, 'Magic_Orb\\VeryShinyMagicOrb.mdx');
        SetUnitScale(ef, 1.5, 1.5, 1.5);
        let arr: unit[] = [];
        this.pool.set(source, arr);
        BaseUtil.onTimer(0.04, (c) => {
            let stp = 60 * (1 - Sin((c / total) * Math.PI));
            stp = Math.min(stp, 80);
            if (c <= total / 2) {
                cx += CosBJ(ag) * stp;
                cy += SinBJ(ag) * stp;
            } else {
                ag = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), cx, cy);
                cx += CosBJ(ag) * -stp;
                cy += SinBJ(ag) * -stp;
            }
            SetUnitX(ef, cx);
            SetUnitY(ef, cy);
            if (c == R2I(total / 2)) {
                arr = [];
                this.pool.set(source, arr);
            }
            let es = SelectUtil.getEnemyUnitsInRange(ef, 85);
            let lv = this.getAbilityLevel(source);
            let atk = UnitCommon.取攻击(source);
            let dmg = 35 * lv + (UnitCommon.取法强(source) + atk * 0.75) * (0.8 * 0.4 * lv);
            for (let e of es) {
                if (arr.includes(e)) continue;
                arr.push(e);
                DamageSystemInstance.applyLightingDamage(e, source, dmg, this.id);
            }
            let us = SelectCommon.getAllyUnitInRange(ef, 85);
            for (let e of us) {
                if (arr.includes(e)) continue;
                arr.push(e);
                DamageSystemInstance.applyHeal(e, source, dmg, this.id);
            }
            let dis = MathUtil.distanceBetweenUnits(ef, source);
            let isEnd = c >= 25 && dis <= stp;
            if (isEnd) {
                KillUnit(ef);
                this.pool.delete(source);
            }
            return !isEnd;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
